﻿package baidu.ui.managers
{
    import baidu.ui.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;

    public class TooltipManager extends Object
    {
        private var trigger2Tooltip:Dictionary;
        private var textField:TextField;
        private var _root:DisplayObjectContainer;
        private var container:Sprite;
        private var _defaultRender:Object = null;
        private var timer:Timer;
        private var delayTimer:Timer;
        private var delayTarget:InteractiveObject;

        public function TooltipManager()
        {
            this.trigger2Tooltip = new Dictionary(true);
            this.timer = new Timer(4000, 1);
            this.delayTimer = new Timer(500, 1);
            this.container = new Sprite();
            this.container.mouseEnabled = false;
            this.container.tabEnabled = false;
            this.container.mouseChildren = false;
            this.textField = new TextField();
            this.textField.background = true;
            this.textField.backgroundColor = 16777164;
            this.textField.border = true;
            this.textField.borderColor = 0;
            this.textField.multiline = false;
            this.textField.wordWrap = false;
            this.textField.autoSize = "left";
            var _loc_1:* = new TextFormat("宋体", 12, 0);
            _loc_1.indent = 2;
            this.textField.setTextFormat(_loc_1);
            this.textField.defaultTextFormat = _loc_1;
            this.timer.addEventListener(TimerEvent.TIMER, this.handleTimer);
            this.delayTimer.addEventListener(TimerEvent.TIMER, this.toolTipDelayHandler);
            return;
        }// end function

        public function get root() : DisplayObjectContainer
        {
            return this._root;
        }// end function

        public function set root(param1:DisplayObjectContainer) : void
        {
            if (this._root == null)
            {
                this._root = param1;
                this._root.addChild(this.container);
            }
            else
            {
                trace("Error:root只允许设置一次，之后不允许修改。");
            }
            return;
        }// end function

        public function get hideDelay() : Number
        {
            return this.timer.delay;
        }// end function

        public function set hideDelay(param1:Number) : void
        {
            this.timer.delay = param1;
            return;
        }// end function

        public function get defaultRender()
        {
            return this._defaultRender;
        }// end function

        public function set defaultRender(param1) : void
        {
            this._defaultRender = param1;
            return;
        }// end function

        private function getRenderInstance(param1)
        {
            var _loc_2:* = undefined;
            if (param1 != null)
            {
                _loc_2 = InstanceManager.createSingletonInstance(param1);
            }
            else if (this.defaultRender != null)
            {
                _loc_2 = InstanceManager.createSingletonInstance(this.defaultRender);
            }
            else
            {
                _loc_2 = this.textField;
            }
            return _loc_2;
        }// end function

        private function showTooltip(param1:String, param2, param3:InteractiveObject) : void
        {
            var _loc_6:Number = NaN;
            var _loc_7:Number = NaN;
            var _loc_8:Number = NaN;
            var _loc_9:Number = NaN;
            var _loc_10:Number = NaN;
            var _loc_11:Number = NaN;
            var _loc_4:* = this.getRenderInstance(param2);
            this.container.addChild(_loc_4);
            _loc_4.text = param1;
            DepthManager.bringToTop(this.container);
            var _loc_5:* = this.container.stage;
            if (this.container.stage != null)
            {
                if (_loc_4 is BUI)
                {
                    (_loc_4 as BUI).drawNow();
                }
                _loc_6 = _loc_5.mouseX;
                _loc_7 = _loc_5.mouseY;
                _loc_8 = _loc_4.width;
                _loc_9 = _loc_4.height;
                _loc_10 = _loc_5.stageWidth;
                _loc_11 = _loc_5.stageHeight;
                if (_loc_6 + _loc_8 + 2 > _loc_10)
                {
                    _loc_4.x = this.container.globalToLocal(new Point(_loc_10, _loc_11)).x - _loc_8 - 2;
                }
                else
                {
                    _loc_4.x = this.container.mouseX;
                }
                if (_loc_7 + _loc_9 + 22 > _loc_11)
                {
                    _loc_4.y = this.container.mouseY - _loc_9;
                }
                else
                {
                    _loc_4.y = this.container.mouseY + 22;
                }
            }
            this.timer.reset();
            this.timer.start();
            return;
        }// end function

        private function hideTooltip(param1) : void
        {
            var _loc_2:* = this.getRenderInstance(param1);
            if (this.container.contains(_loc_2))
            {
                this.container.removeChild(_loc_2);
            }
            this.timer.reset();
            this.timer.stop();
            return;
        }// end function

        public function register(param1:InteractiveObject, param2:String, param3 = null) : void
        {
            if (!this._root)
            {
                this.root = param1.stage;
            }
            if (param2 != null)
            {
                this.trigger2Tooltip[param1] = {text:param2, render:param3};
                param1.addEventListener(MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
                param1.addEventListener(MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
                param1.addEventListener(MouseEvent.MOUSE_DOWN, this.handleTriggerDown, false);
            }
            else
            {
                param1.removeEventListener(MouseEvent.ROLL_OVER, this.handleTriggerOver, false);
                param1.removeEventListener(MouseEvent.ROLL_OUT, this.handleTriggerOut, false);
                param1.removeEventListener(MouseEvent.MOUSE_DOWN, this.handleTriggerDown, false);
                delete this.trigger2Tooltip[param1];
                if (this.delayTarget && this.delayTarget == param1)
                {
                    this.delayTimer.stop();
                    this.delayTarget = null;
                }
            }
            return;
        }// end function

        public function unregister(param1:InteractiveObject) : void
        {
            this.register(param1, null);
            return;
        }// end function

        private function handleTriggerOver(event:MouseEvent) : void
        {
            if (event.buttonDown)
            {
                return;
            }
            this.delayTarget = event.target as InteractiveObject;
            this.delayTimer.reset();
            this.delayTimer.start();
            return;
        }// end function

        private function handleTriggerOut(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget as InteractiveObject;
            var _loc_3:* = this.trigger2Tooltip[_loc_2]["render"];
            if (this.delayTarget)
            {
                this.delayTimer.stop();
                this.delayTarget = null;
            }
            else
            {
                this.hideTooltip(_loc_3);
            }
            return;
        }// end function

        private function handleTriggerDown(event:MouseEvent) : void
        {
            this.handleTriggerOut(event);
            return;
        }// end function

        private function handleTimer(event:TimerEvent) : void
        {
            if (this.container.numChildren)
            {
                this.container.removeChildAt(0);
            }
            return;
        }// end function

        private function toolTipDelayHandler(event:TimerEvent) : void
        {
            if (!this.delayTarget)
            {
                return;
            }
            var _loc_2:* = this.delayTarget;
            var _loc_3:* = this.trigger2Tooltip[_loc_2]["text"];
            var _loc_4:* = this.trigger2Tooltip[_loc_2]["render"];
            this.showTooltip(_loc_3, _loc_4, _loc_2);
            this.delayTimer.stop();
            this.delayTarget = null;
            return;
        }// end function

        public static function getInstance() : TooltipManager
        {
            return InstanceManager.createSingletonInstance();
        }// end function

    }
}
